//
//  CYVideoDetail_CompanyTableViewCell.m
//  cyydt
//
//  Created by chu on 2019/1/23.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYVideoDetail_CompanyTableViewCell.h"


@implementation CYVideoDetail_CompanyTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.huoquBtn.layer.cornerRadius =  self.huoquBtn.frame.size.height / 2;
    self.huoquBtn.layer.borderColor = RGBOF(0x666666).CGColor;
    self.huoquBtn.layer.borderWidth = 1;
    self.huoquBtn.layer.masksToBounds = YES;
}

- (void)setCompanymodel:(CYProjectModel *)companymodel{
    _companymodel = companymodel;
    self.huoquBtn.hidden = YES;
    self.huoquBtn.userInteractionEnabled = NO;
    self.arrowImageView.hidden = NO;
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:companymodel.company_logo] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];
    self.titleLabel.text = companymodel.company_name;
    if ([NSString stringIsNull:companymodel.item_title]) {
        self.introLabel.text = companymodel.slogan;
    }else{
        self.introLabel.text = companymodel.item_title;
    }
    
}

- (void)setModel:(CYProjectModel *)model{
    _model = model;
    self.huoquBtn.hidden = NO;
    self.arrowImageView.hidden = YES;
    self.huoquBtn.userInteractionEnabled = YES;
    
    self.headImageView.image = [UIImage imageNamed:@"椭圆 6"];
    self.titleLabel.text = @"免费获取加盟方案";
    self.introLabel.text = @"项目经理 7x24h 1对1为您服务";
}

- (IBAction)huoquAction:(UIButton *)sender {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    //获取资料
    NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"CYProjectDetail_PhonePopView" owner:self options:nil];
    CYProjectDetail_PhonePopView *backView = [nibView objectAtIndex:0];
    self.ziliaoView = backView;
    backView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    __weak typeof(self)weakself = self;
    backView.block = ^(NSString * _Nonnull name, NSString * _Nonnull phone, NSString * _Nonnull remark) {
        NSLog(@"name -- %@",name);
        [weakself LiuYanWithName:name Phone:phone Remark:remark];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    };
    [backView show];
}

- (void)LiuYanWithName:(NSString *)name Phone:(NSString *)phone Remark:(NSString *)remark{
    if (name.length == 0) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请填写您的姓名" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (phone.length == 0) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请填写您的手机号" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (remark.length == 0) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"留言不能为空" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    [EasyShowLodingView showLoding];
    __weak typeof(self)weakself = self;
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"guest-book/create"];
    NSDictionary *param = @{@"id":self.model.ID, @"type":@"28", @"truename":name, @"mobile":phone, @"content":remark, @"gfrom":@"14"};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"留言板 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            //            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [weakself.ziliaoView dismiss];
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

#pragma mark - UIKeyboardNotification

- (void)keyboardWillShow:(NSNotification *)notification
{
    CGRect endRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    __block CGFloat y = kWindowH - CGRectGetMaxY(self.ziliaoView.backView.frame) + 50 - endRect.size.height;
    __weak typeof(self)weakself = self;
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        if (y < 0) {
            weakself.ziliaoView.y = y;
        }
        [weakself layoutIfNeeded];
    } completion:nil];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    __weak typeof(self)weakself = self;
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        weakself.ziliaoView.y = kWindowH - weakself.ziliaoView.height;
        [weakself layoutIfNeeded];
    } completion:nil];
}


@end
